{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4bcda43c",
   "metadata": {},
   "source": [
    "* 12-12-02"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1dc4c31",
   "metadata": {},
   "source": [
    "# 函数的定义\n",
    "## 1.函数的定义\n",
    ">1. shuxue fx=ax+b\n",
    ">2. python def=fib(x)\n",
    ">3. 编程 数学\n",
    ">>1. 函数可以被定义名称 定义的关键字 def\n",
    ">>2. 数学是写在同一行 （定义函数 和运算公式）   面向对象的编程中 函数写多行 定义函数为单独一行\n",
    ">>3. 数学函数一般没有注释 \n",
    ">>4. 数学中：ax+b是计算公式，而编程过程中 我们将函数的内容变为**函数体** 函数体要有**缩进**\n",
    ">>5.数学函数执行 将参数带入公式 编程中函数执行 将参数带入函数体\n",
    "## 2.关于函数的更多定义 \n",
    "  > 1. 函数的参数\n",
    ">> 1. 形式参数\n",
    ">> 2. 实际参数\n",
    ">> 3. 默认参数\n",
    ">> 4. 位置参数\n",
    ">> 5. 关键字参数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20917edd",
   "metadata": {},
   "source": [
    ">1.定义函数\n",
    "```\n",
    "def: 函数的关键字\n",
    "fib: (函数名）\n",
    "(n): 定义函数的参数\n",
    "```\n",
    ">代码部分：\n",
    "```\n",
    "   a, b = 0, 1\n",
    "     while a < n:\n",
    "        print(a, end=' ')\n",
    "         a, b = b, a+b\n",
    "     print()\n",
    "```\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 306,
   "id": "c403b7e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 斐波那切数列:0 ,1,,1,2,3,5,8,13,21.....\n",
    "def fib(x):\n",
    "    \"\"\"列出指定x以内的斐波那切数列\"\"\"\n",
    "    a,b = 0,1\n",
    "    while a<x :\n",
    "        print(a,end=\" \")\n",
    "        a,b = b,a+b\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 307,
   "id": "8d5c40c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 2 3 5 8 13 21 34 0 1 1 2 3 5 8 13 21 34 "
     ]
    }
   ],
   "source": [
    "fib(50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 308,
   "id": "f6d3caa1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践 请大家写一个函数 这个函数可以打印 “你好 xx”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 309,
   "id": "c9b55a32",
   "metadata": {},
   "outputs": [],
   "source": [
    "def hello(name):\n",
    "\n",
    "    \"\"\"显示问候语\"\"\"\n",
    "    print(\"你好，%s同学~\"%(name))\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 310,
   "id": "9e1c80cd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好，ppp同学~\n",
      "你好，ppp同学~\n"
     ]
    }
   ],
   "source": [
    "hello(\"ppp\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 311,
   "id": "fd54d9c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 尝试input()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c6b0077",
   "metadata": {},
   "source": [
    ">调用函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 312,
   "id": "ea70a51d",
   "metadata": {},
   "outputs": [],
   "source": [
    "def hello(qwe):\n",
    "    \"\"\"显示问候语\"\"\"\n",
    "    print(\"hello world\", (qwe))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 313,
   "id": "e4636e45",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world qqqq\n",
      "hello world qqqq\n"
     ]
    }
   ],
   "source": [
    "hello(\"qqqq\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 314,
   "id": "efbcd2d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 288,
   "id": "bef07794",
   "metadata": {},
   "outputs": [],
   "source": [
    "time.sleep(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 289,
   "id": "ac777586",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "time.struct_time(tm_year=2021, tm_mon=12, tm_mday=9, tm_hour=0, tm_min=26, tm_sec=21, tm_wday=3, tm_yday=343, tm_isdst=0)"
     },
     "execution_count": 262,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": "time.struct_time(tm_year=2021, tm_mon=12, tm_mday=9, tm_hour=0, tm_min=26, tm_sec=24, tm_wday=3, tm_yday=343, tm_isdst=0)"
     },
     "execution_count": 289,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.localtime()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b398154",
   "metadata": {},
   "source": [
    "# 函数的参数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aca24937",
   "metadata": {},
   "source": [
    ">1. 形式参数\n",
    ">>1. 定义函数的时候定义的参数\n",
    ">>2. 形式参数不占用内存\n",
    "> 2. 实际参数\n",
    ">>1. 在调用 函数的时候传递的参数\n",
    ">>2. 调用函数，形式参数分配空间，变为实际参数，执行函数内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 290,
   "id": "3d8ad277",
   "metadata": {},
   "outputs": [],
   "source": [
    "def hello(name):\n",
    "    \"\"\"显示问候语\"\"\"\n",
    "    print(\"你好，%s同学~\"%(name))\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 291,
   "id": "142d68cc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好，11同学~\n",
      "你好，11同学~\n"
     ]
    }
   ],
   "source": [
    "hello(\"11\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 292,
   "id": "03176dae",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 课本 \n",
    "def search4vowels(word):\n",
    "    vowels = set(\"aeiou\")\n",
    "    word = set(word)\n",
    "    print(vowels&word)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 293,
   "id": "e3cce529",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'u', 'i', 'o', 'e'}\n",
      "{'u', 'i', 'o', 'e'}\n"
     ]
    }
   ],
   "source": [
    "search4vowels(\"8qwertyuiop\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 294,
   "id": "0b37b7d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 形式参数 word\n",
    "# 实际参数 wwwwwwwwdjvaemio;boabi4\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 295,
   "id": "3ec66fce",
   "metadata": {},
   "outputs": [],
   "source": [
    "#函数的优势\n",
    "#1.可以重复使用 \n",
    "#2. 使用者可以不了解函数本身\n",
    "#3. 方便"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08ee58ad",
   "metadata": {},
   "source": [
    "* 在多个参数的情况下\n",
    ">1. 位置参数\n",
    ">> 1. 在形式参数的定义中，有一定的先后顺序\n",
    ">> 2. 在调用函数时，要考虑参数的先后顺序\n",
    ">> 3. 调用函数时，不仅要考虑参数位置，还要考虑参数个数\n",
    ">2. 关键字参数\n",
    ">3. 默认参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 296,
   "id": "4386c911",
   "metadata": {},
   "outputs": [],
   "source": [
    "def describe_pet(pet_name,animal_type):\n",
    "    \"\"\"显示宠物信息\"\"\"\n",
    "    print(\"I have a \" + animal_type + \".\")\n",
    "    print(\"My \" + animal_type + \"'s name is \"+pet_name.title() + \".\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 297,
   "id": "38ced708",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I have a 豆豆.\n",
      "My 豆豆's name is Dog.\n",
      "I have a 豆豆.\n",
      "My 豆豆's name is Dog.\n"
     ]
    }
   ],
   "source": [
    "describe_pet(\"dog\",\"豆豆\")\n",
    "# 不能乱顺序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 298,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I have a pig.\n",
      "My pig's name is Qq.\n",
      "I have a pig.\n",
      "My pig's name is Qq.\n"
     ]
    }
   ],
   "source": [
    "describe_pet(\"qq\",\"pig\")\n",
    "\n",
    "## 默认参数 设定一定的参数\n",
    "def describe_pet(pet_name,animal_type=\"dog\"):\n",
    "    \"\"\"显示宠物信息\"\"\"\n",
    "    print(\"\\nI have a \" + animal_type + \".\")\n",
    "    print(\"My \" + animal_type + \"'s name is \"+pet_name + \".\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 299,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a dog.\n",
      "My dog's name is qq.\n",
      "\n",
      "I have a dog.\n",
      "My dog's name is qq.\n"
     ]
    }
   ],
   "source": [
    "describe_pet('qq')\n",
    "# 可以不打dog 种类参数"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 300,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a cat.\n",
      "My cat's name is qq.\n",
      "\n",
      "I have a cat.\n",
      "My cat's name is qq.\n"
     ]
    }
   ],
   "source": [
    "describe_pet(\"qq\",\"cat\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 300,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 300,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "------\n",
    "* 注意事项\n",
    "1. 位置参数和关键字参数同时使用（调用函数）\n",
    ">> 1. positional argument follows keyword argument（关键字参数<font style=\"color:red\">均</font>应该放在位置参数之后）\n",
    ">> 2. 只要满足1的条件，剩余的关键字参数（多个）不考虑先后顺序"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 301,
   "outputs": [],
   "source": [
    "def test(x,y,z):\n",
    "    \"\"\"打印x,y,z的结果\"\"\"\n",
    "    print(x)\n",
    "    print(y)\n",
    "    print(z)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 302,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n",
      "7\n",
      "7\n",
      "7\n",
      "7\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "test(7,7,7)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 303,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "2\n",
      "2\n",
      "5\n",
      "2\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# 实践1 关键参数参要在最后：\n",
    "test(5,2,z = 2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 304,
   "outputs": [],
   "source": [
    "# 测试2： 默认参只能在最后\n",
    "def test(x,z,y=1,): # y为\n",
    "    \"\"\"打印x,y,z的结果\"\"\"\n",
    "    print(x)\n",
    "    print(y)\n",
    "    print(z)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 305,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "1\n",
      "2\n",
      "2\n",
      "1\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "test( 2, 2, y=1)\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "* 总结\n",
    "* 等于号的参数只能放在最后的位置 函数表达的时候"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}